package com.mg.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mg.yygh.common.exception.YyghException;
import com.mg.yygh.common.result.Result;
import com.mg.yygh.common.utils.MD5;
import com.mg.yygh.hosp.service.HospitalSetService;
import com.mg.yygh.model.hosp.HospitalSet;
import com.mg.yygh.vo.hosp.HospitalQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author M_Dragon
 * @date 2022/8/23/023 14:36
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    //1 查询所有医院记录
    @ApiOperation(value = "查询所有医院记录")
    @GetMapping("/findAll")
    public Result findAllHospitalSet() {
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }
    //2 删除记录
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable("id") Integer id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件分页查询
    @ApiOperation(value = "条件分页查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") Long current,
                                  @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) HospitalQueryVo hospitalQueryVo
    ) {
        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        String hosname = hospitalQueryVo.getHosname();
        String hoscode = hospitalQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hoscode)) {
            hospitalSetQueryWrapper.eq("hoscode", hoscode);
        }
        if (!StringUtils.isEmpty(hosname)) {
            hospitalSetQueryWrapper.like("hosname", hosname);
        }
        Page<HospitalSet> hosSetPage = hospitalSetService.page(hospitalSetPage, hospitalSetQueryWrapper);
        return Result.ok(hosSetPage);

    }

    //4 添加医院设置数据
    @ApiOperation(value = "添加医院设置数据")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    //5 根据id获取HospitalSet一条数据
    @ApiOperation(value = "根据id获取HospitalSet一条数据")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet != null) {
            return Result.ok(hospitalSet);
        } else {
            return Result.fail();
        }
    }

    //6 根据id修改更新HospitalSet表数据
    @ApiOperation(value = "根据id修改更新HospitalSet表数据")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }


    //7 通过id批量逻辑删除
    @ApiOperation(value = "通过id批量逻辑删除")
    @DeleteMapping("BatchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "医院设置锁定，解锁-status")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return Result.ok();

    }

    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKeyHospitalSet(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // todo 发送短信
        try {
            int a = 1 / 0;
        }catch (Exception e){
            throw new YyghException("出错啦",201);
        }
        return Result.ok();
    }




}
